package com.wt.struggle.goodbye.sq.adapter

import android.content.Context
import android.graphics.BitmapFactory
import android.media.Image
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ProBaseAdapter
import com.wt.struggle.goodbye.info.NewsInfo
import com.xin.lv.yang.utils.utils.ImageUtil
import kotlinx.android.synthetic.main.hand_pick_item.view.*

class HandPickAdapter(context: Context, list: ArrayList<NewsInfo>) : ProBaseAdapter<NewsInfo>(context, list) {

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val vh = holder as VH
        val info = list[position]

        ImageUtil.getInstance().loadRoundCircleImage(context, vh.pickImage, info.iconUrl, 0, 4)
        vh.pickName.text = info.name
        vh.pickContent.text = info.des

        vh.tvCollectNumber.text = if (info.numberOfCircleCollected != null && info.numberOfCircleCollected != 0) {
            info.numberOfCircleCollected.toString()
        } else {
            "0"
        }

        vh.tvCommentNumber.text = if (info.numberOfCommented != null && info.numberOfCommented != 0) {
            info.numberOfCommented.toString()
        } else {
            "0"
        }


        if (info.isCollection != 0) {
            vh.imgImageColl.setImageBitmap(BitmapFactory.decodeResource(context.resources, R.drawable.coll_y))
        } else {
            vh.imgImageColl.setImageBitmap(BitmapFactory.decodeResource(context.resources, R.drawable.coll_n))
        }

        vh.imgImageColl.setOnClickListener {
            if (collectOrComment != null) {
                collectOrComment!!.onCollect(vh.adapterPosition)
            }

        }

        vh.imgImageComment.setOnClickListener {
            if (collectOrComment != null) {
                collectOrComment!!.onComment(vh.adapterPosition)
            }
        }


    }


    override fun onCreateView(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = inflater.inflate(R.layout.hand_pick_item, parent, false)
        return VH(view)

    }

    override fun onUpdateHolder(holder: RecyclerView.ViewHolder, position: Int, payloads: MutableList<Any>) {

    }


    fun change(clickPosition: Int) {
        val info = list[clickPosition]

        if (info.isCollection == 1) {
            info.isCollection = 0
            if (info.numberOfCircleCollected != 0) {
                info.numberOfCircleCollected = info.numberOfCircleCollected!! - 1
            }
        } else {
            info.isCollection = 1
            info.numberOfCircleCollected = info.numberOfCircleCollected!! + 1
        }

        notifyItemChanged(clickPosition)

    }


    inner class VH(view: View) : RecyclerView.ViewHolder(view) {
        val pickImage = view.pickImage
        val pickName = view.pickName
        val pickContent = view.pickContent
        val imgImageColl = view.imgImageColl
        val tvCollectNumber = view.tvCollectNumber
        val imgImageComment = view.imgImageComment
        val tvCommentNumber = view.tvCommentNumber

    }

    var collectOrComment: OnCollectOrCommentListener? = null

    interface OnCollectOrCommentListener {
        fun onCollect(position: Int)
        fun onComment(position: Int)
    }
}